package com.event.config;

import com.event.interceptor.JwtTokenUserInterceptor;
import com.event.json.JacksonObjectMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
	
	@Resource
	private JwtTokenUserInterceptor jwtTokenUserInterceptor;
	
	/**
	 * 注册自定义拦截器
	 */
	protected void addInterceptors(InterceptorRegistry registry) {
		log.info("注册自定义拦截器...");
		// 排除用户
		registry.addInterceptor(jwtTokenUserInterceptor)
			.addPathPatterns("/**")
			.excludePathPatterns("/user/login", "/user/register");
	}
	
	
	/**
	 * 消息转换器
	 */
	public void extendMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
		log.info("拓展消息转换器...");
		// 创建消息转换器对象
		MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
		// 将设置的消息转换器加入容器,0设置优先级。消息转换器设置对象转换器，对象转换器将java对象序列化为json数据
		converter.setObjectMapper(new JacksonObjectMapper());
		messageConverters.add(0, converter);
	}
	
}
